<?php

class invoice extends CActiveRecord {
/**
 * The followings are the available columns in table 'invoice':
 * @var double $do_id
 * @var double $amt
 * @var double $date
 * @var double $id
 * @var tinyint $ref_type
 * @var var(30) $ref_no
 */
    CONST PO = 1, INV = 2, DN = 3, OTHER = 4, PL = 0;
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'invoice';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        return array(
        array('do_id, ref_type', 'numerical'),
        array('ref_no','length','max'=>30),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
        return array(
        'details'=>array(self::HAS_MANY,'invoice_details','inv_id'),
        'do'=>array(self::BELONGS_TO,'cargo','do_id')
        );
    }
    public function saveMultiple() {
        $original_ref_no = $this->ref_no;
        $ref_nos = explode("\n",$this->ref_no);
        print_r($ref_nos);
        foreach($ref_nos as $ref_no) {
            if(!empty($ref_no)) {
                $this->ref_no = $ref_no;
                if(!$this->save())
                    return false;
            }
        }
        $this->ref_no = $original_ref_no;
        return true;
    }
    // Return Ref Type string

    public function getRefType($ref) {
        switch($ref) {
            case 0: return 'Packing List';
                break;
            case 1: return 'Purchase Order';
                break;
            case 2: return 'Invoice No.';
                break;
            case 3: return 'Delivery No.';
                break;
            case 4:
            default: return 'Other Ref';
                break;
        }
    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
        'ref_no'=>'Reference',
        'id'=>'Id',
        );
    }
}